package com.songlanyun.modules.donationpointsconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.donationpointsconfig.dao.DonationPointsConfigDao;
import com.songlanyun.modules.donationpointsconfig.entity.DonationPointsConfigEntity;
import com.songlanyun.modules.donationpointsconfig.service.DonationPointsConfigService;
import com.songlanyun.modules.exception.ConfigException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.Query;
import org.springframework.transaction.annotation.Transactional;


@Service("donationPointsConfigService")
public class DonationPointsConfigServiceImpl extends ServiceImpl<DonationPointsConfigDao, DonationPointsConfigEntity> implements DonationPointsConfigService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DonationPointsConfigEntity> page = this.page(
                new Query<DonationPointsConfigEntity>().getPage(params),
                new QueryWrapper<DonationPointsConfigEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveList(List<DonationPointsConfigEntity> list) {
        this.baseMapper.delete(new LambdaQueryWrapper<>());
        list.forEach(c -> {
            ValidatorUtils.validateEntity(c);
        });

        this.saveBatch(list);

    }

    @Override
    public DonationPointsConfigEntity loadById(Long id, boolean tw) {
        DonationPointsConfigEntity entity = this.baseMapper.selectById(id);
        if (ObjectUtil.isNull(entity) && tw) {
            throw new RRException(ConfigException.THE_DONATION_RULE_CANNOT_BE_NULL);
        }
        return entity;
    }

}
